package com.sk.sourcecircle.activity.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.login.LoginActivity;
import com.sk.sourcecircle.adapter.GroupListAdapter;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.dialog.TipDialogFragment;
import com.sk.sourcecircle.entity.GroupListBean;
import com.sk.sourcecircle.entity.HomeTabList;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/12.
 */

public class GroupListActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.group_name)
    TextView groupKind;

    private int cateId;
    private List<GroupListBean.DataBean> allDataList = new ArrayList<>();
    private GroupListAdapter adapter;
    private boolean isLoadMore = false;
    private int page = 1;

    HomeTabList.DataBean dataBean;
    private LoadingDialog loadingDialog;

    private int postion;

    @Override
    protected int getContentView() {
        return R.layout.activity_group_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataBean = getIntent().getParcelableExtra("data");

        if (dataBean != null) {
            groupKind.setText(dataBean.getCate_name() + "社群");
            cateId = dataBean.getCate_id();
        }

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new GroupListAdapter(R.layout.item_group, allDataList);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

        loadingDialog.show();
        AppApi.getGroupList(String.valueOf(cateId), MyApplication.getInstance().getLng(), MyApplication.getInstance().getLat(), page, this, 0, this);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                GroupListBean groupListBean = JSON.parseObject(result, GroupListBean.class);
                List<GroupListBean.DataBean> dataBeanList = groupListBean.getData();
                allDataList.addAll(dataBeanList);
                if (dataBeanList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();
                break;
            case 1:
                allDataList.get(postion).setFollow(1);
                adapter.notifyItemChanged(postion);
                SnackUtils.show(mRootView,"关注成功！");
                break;
            case 2:
                allDataList.get(postion).setFollow(0);
                adapter.notifyItemChanged(postion);
                SnackUtils.show(mRootView,"取关成功！");
                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }


    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getGroupList(String.valueOf(cateId), MyApplication.getInstance().getLng(), MyApplication.getInstance().getLat(), page, this, 0, this);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.item_bg:
                Intent intent = new Intent(mContext, GroupDetailsActivity.class);
                intent.putExtra("id", allDataList.get(position).getCommunity_id());
                startActivity(intent);
                break;
            case R.id.add_care:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent loginIntent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(loginIntent, 200);
                    return;
                }
                this.postion = position;
                loadingDialog.show();
                AppApi.addCareGroup(allDataList.get(position).getCommunity_id(), GroupListActivity.this, 1, GroupListActivity.this);
                break;
            case R.id.cancel_care:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent loginIntent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(loginIntent, 200);
                    return;
                }
                this.postion = position;
                TipDialogFragment tipDialogFragment = new TipDialogFragment();
                tipDialogFragment.setContent("确认取消关注此社群？");
                tipDialogFragment.setMyCallBack(GroupListActivity.this);
                tipDialogFragment.show(getSupportFragmentManager(), "tip");
                break;
        }
    }

    @Override
    public void sure() {
        super.sure();
        AppApi.cancelCareGroup(allDataList.get(postion).getCommunity_id(), GroupListActivity.this, 2, GroupListActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            if (requestCode == 200) {
                page = 1;
                allDataList.clear();
                loadingDialog.show();
                AppApi.getGroupList(String.valueOf(cateId), MyApplication.getInstance().getLng(), MyApplication.getInstance().getLat(), page, this, 0, this);
            }
        }
    }
}
